package com.smh.a04MapDemo5;

import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeMap;

public class TreeMapDemo3 {
    public static void main(String[] args) {

        String s = "aasdadsadasddsvxcvxzxcaqwewqweqeq";

        //有序用LinkHashMap
        //排序用TreeMap
        //不排序用HashMap
        TreeMap<Character,Integer> tm = new TreeMap<>();


        char[] arr = s.toCharArray();
        for (char c : arr) {
            if (tm.containsKey(c)){
                int value = tm.get(c);
                value++;
                tm.put(c,value);
            }else {
                tm.put(c,1);
            }

        }

        tm.forEach((key,value)-> System.out.println(key+"="+value));
        System.out.println("----------------------");


        Set<Map.Entry<Character, Integer>> entries = tm.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {

            System.out.println(entry.getKey()+"="+entry.getValue());
        }

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<Character, Integer> entry : entries) {
            sb.append(entry.getKey()+" ("+entry.getValue()+") ");
        }
        System.out.println(sb.toString());

        StringJoiner sj = new StringJoiner("","","");
        for (Map.Entry<Character, Integer> entry : entries) {
            sj.add(entry.getKey()+" ("+entry.getValue()+") ");
        }
        System.out.println(sj.toString());


    }
}
